package com.qf.controller;

import com.qf.SelfClass.Citys;
import com.qf.SelfClass.ResponseBody2;
import com.qf.SelfClass.ResponseBody3;
import com.qf.SelfClass.ResponseOrder;
import com.qf.pojo.*;
import com.qf.service.OrderService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Mr.Liu
 * @date 2021/12/16 11:15
 */
@RequestMapping("/order")
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询取车和还车地点
     * @param getid
     * @param backid
     * @param cid
     * @return
     */
    @ResponseBody
    @RequestMapping("/findTwoCity")
    public ResponseBody2 findTwoCity(@RequestParam("getid") Integer getid,
                                     @RequestParam("backid") Integer backid,
                                     @RequestParam("cid") Integer cid){
        City getCity = orderService.findCityByGetid(getid);
        City backCity = orderService.findCityByBackid(backid);
        /*System.out.println(getCity);
        System.out.println(backCity);*/
        Citys citys = new Citys();
        citys.setGetCity(getCity);
        citys.setBackCity(backCity);
        if(getCity!=null && backCity!=null){
            return new ResponseBody2(1,citys);
        }
        return null;
    }

    /**
     * 通过id查询车辆
     * @param getid
     * @param backid
     * @param cid
     * @return
     */
    @ResponseBody
    @RequestMapping("/findCarByid")
    public ResponseBody3 findCarById(@RequestParam("getid") Integer getid,
                                     @RequestParam("backid") Integer backid,
                                     @RequestParam("cid") Integer cid){
        /*System.out.println(cid);*/
        Car car = orderService.findCarById(cid);
        //System.out.println(car);
        if (car!=null){
            return new ResponseBody3(1,car);
        }
        return null;
    }
    @ResponseBody
    @RequestMapping("/insertOrder")
    public ResponseOrder insertOrder(@RequestParam("getid") Integer getid,
                                     @RequestParam("backid") Integer backid,
                                     @RequestParam("cid") Integer cid,
                                     @RequestParam("oprice") Double oprice,
                                     HttpServletRequest request){

        User user = (User) request.getSession().getAttribute("user");
        String status = "已预订";
        Order order = new Order();
        order.setGetid(getid);
        order.setBackid(backid);
        order.setCid(cid);
        order.setOprice(oprice);
        order.setStatus(status);
        if(user!=null){
            Integer uid = user.getId();
            order.setUid(uid);
        }
        //System.out.println(order);
        Integer integer = orderService.insertOrder(order);
        if(integer!=null){
            return new ResponseOrder("预订成功,可在我的订单查看!");
        }
        return new ResponseOrder("预定失败");
    }

}
